clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* Add forward / lagged returns 
use $Work/msf_adjusted, clear

tsset permno datem
gen  LretMo1=1+ret
gen  LretQr1=(1+ret)*(1+L1.ret)*(1+L2.ret)
gen  LretYr1=1
gen  LretYr3=1
gen  LretYr5=1
forvalues x=0(1)11 {
	replace LretYr1= LretYr1*(1+L`x'.ret )
}
forvalues x=0(1)35 {
	replace LretYr3= LretYr3*(1+L`x'.ret )
}
forvalues x=0(1)59 {
	replace LretYr5= LretYr5*(1+L`x'.ret )
}

gen  retMo1=(1+F.ret)
gen  retQr1=(1+F.ret)*(1+F2.ret)*(1+F3.ret)
gen  retYr1=1
gen  retYr3=1
gen  retYr5=1

forvalues x=1(1)12 {
	replace retYr1= retYr1*(1+F`x'.ret )
}
forvalues x=1(1)36 {
	replace retYr3= retYr3*(1+F`x'.ret )
}
forvalues x=1(1)60 {
	replace retYr5= retYr5*(1+F`x'.ret )
}
keep permco permno datem Lret* ret*
* *****************************************************
merge 1:1 permco permno datem using $Work/Firms01_June, keepusing(permco permno datem LTGagg LTG BOOK PROF INV SI* MOME bemeLTM)
keep if _merge==3
drop _merge SIZE
rename SI10 SIZE
drop if bemeLTM<=0
gen lnbeme=ln(bemeLTM)
* *****************************************************
merge 1:1 permno permco datem using $Work/street_mthly

tsset permno datem
gen eps=streetLTM/shsout
gen sps=  saleLTM/shsout

gen gepsLMo1=ln(eps)-ln(L1.eps )
gen gepsLQr1=ln(eps)-ln(L3.eps )
gen gepsLYr1=ln(eps)-ln(L12.eps)
gen gepsLYr3=ln(eps)-ln(L36.eps)
gen gepsLYr5=ln(eps)-ln(L60.eps)

gen gspsLMo1=ln(sps)-ln(L1.sps)
gen gspsLQr1=ln(sps)-ln(L3.sps)
gen gspsLYr1=ln(sps)-ln(L12.sps)
gen gspsLYr3=ln(sps)-ln(L36.sps)
gen gspsLYr5=ln(sps)-ln(L60.sps)

gen geLMo1=ln(streetLTM)-ln(L1.streetLTM )
gen geLQr1=ln(streetLTM)-ln(L3.streetLTM )
gen geLYr1=ln(streetLTM)-ln(L12.streetLTM)
gen geLYr3=ln(streetLTM)-ln(L36.streetLTM)
gen geLYr5=ln(streetLTM)-ln(L60.streetLTM)

gen gsLMo1=ln(saleLTM)-ln(L1.saleLTM)
gen gsLQr1=ln(saleLTM)-ln(L3.saleLTM)
gen gsLYr1=ln(saleLTM)-ln(L12.saleLTM)
gen gsLYr3=ln(saleLTM)-ln(L36.saleLTM)
gen gsLYr5=ln(saleLTM)-ln(L60.saleLTM)

drop if _merge==2
drop _merge sps eps
* *****************************************************
foreach t in Mo1 Qr1 Yr1 Yr3 Yr5 {
	gen lnret`t'=ln(ret`t')
}

gstats winsor gepsL* gspsL* lnbeme, cuts(1 99) replace
foreach var of varlist gsps* geps* lnbeme  {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}

gen gsps=.
gen geps=.
gen LR=.

eststo clear
foreach t in Mo1 Yr1 Yr5 {
replace geps=gepsL`t'
replace LR=Lret`t'

local lag=1*index("`t''", "Mo1")+4*index("`t''", "Qr1")+13*index("`t''", "Yr1")+37*index("`t''", "Yr3")+61*index("`t''", "Yr5") 
display `lag'
quietly: eststo: ivreghdfe lnret`t' 	geps  			, dkraay(`lag') a(permco datem)
quietly: eststo: ivreghdfe lnret`t' 	geps  LR		, dkraay(`lag') a(permco datem)
quietly: eststo: ivreghdfe lnret`t' 	geps  lnbeme		, dkraay(`lag') a(permco datem)

local file "$Tables/Table10_June.csv"
esttab	using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) title("time & firm fxd effect") replace
}
eststo clear
foreach t in Mo1 Yr1 Yr5 {
replace gsps=gspsL`t'
replace LR=Lret`t'

local lag=1*index("`t''", "Mo1")+4*index("`t''", "Qr1")+13*index("`t''", "Yr1")+37*index("`t''", "Yr3")+61*index("`t''", "Yr5") 
display `lag'
quietly: eststo: ivreghdfe lnret`t' 	gsps  		, dkraay(`lag') a(permco datem)
quietly: eststo: ivreghdfe lnret`t' 	gsps  LR	, dkraay(`lag') a(permco datem)
quietly: eststo: ivreghdfe lnret`t' 	gsps  lnbeme	, dkraay(`lag') a(permco datem)
}
local file "$Tables/Table10_June.csv"
esttab	using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) title("time & firm fxd effect") append
